package com.dotflat.agreement.controller;

import com.dotflat.agreement.domain.OPAgreementRecord;
import com.dotflat.agreement.po.OPAgreementRecordPO;
import com.dotflat.agreement.service.OPAgreementRecordService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.controller.RestfulBaseController;
import com.starinside.wp.domain.Children;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/v2/agreement_record")
@Slf4j
@Api(tags = "运营 - 协议和授权记录")
public class OPAgreementRecordController extends RestfulBaseController<OPAgreementRecord, OPAgreementRecordService, OPAgreementRecordPO> {

    @Autowired
    public void setBaseService(OPAgreementRecordService service){
        super.setBaseService(service);
    }


    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    public StatusResponse add(@RequestHeader(TICKET) String ticket, HttpServletRequest req, OPAgreementRecordPO param){

        String childId = validChildrenId(null, req);
        Children children = childrenService.getById(childId);

        OPAgreementRecord ret = baseService.add(children.getAreaId(), children.getId(), param);

        List<OPAgreementRecord> tmp = new ArrayList<>();
        tmp.add(ret);

        return StatusResponse.success(tmp);
    }

//    @ApiOperation(value = "获取一个对象")
//    @GetMapping()
//    public StatusResponse get(@RequestHeader(TICKET) String ticket, HttpServletRequest req, String id){
//
//        String childId = validChildrenId(null, req);
//        Children children = childrenService.getById(childId);
//
//        return StatusResponse.success(baseService.get(children.getAreaId(), null, id));
//    }

    @ApiOperation(value = "逻辑删除一个或多个对象")
    @DeleteMapping()
    public StatusResponse delete(@RequestHeader(TICKET) String ticket, HttpServletRequest req, String[] ids, String id ){

        String childId = validChildrenId(null, req);
        Children children = childrenService.getById(childId);

        if (StringUtils.isNotEmpty(id)) {
            AreableObjectBasePO param = new AreableObjectBasePO();
            param.setId(id);

            baseService.delete(children.getAreaId(), children.getId(), param);
        }else if(ids != null && ids.length > 0){
            baseService.delete(children.getAreaId(), children.getId(), ids);
        }else{
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK, "参数错误!");
        }

        return StatusResponse.success();
    }
}
